<?php

class m_pinjaman_palapa extends CI_Model {

    var $palapa_client;

    function __construct() {
        parent::__construct();
        $this->palapa_client = $this->load->database('palapa_client', TRUE);
    }

    public function get($where = null, $limit = null, $offset = null) {
        $this->palapa_client->select("anggota.*");
        $this->palapa_client->select("pinjaman.*");
        $this->palapa_client->select("jenis_kredit.*");

        $this->palapa_client->join("anggota", "anggota.ID_ANGGOTA = pinjaman.ID_ANGGOTA");
        $this->palapa_client->join("jenis_kredit", "jenis_kredit.ID_JENIS_KREDIT = pinjaman.ID_JENIS_KREDIT");

        if($where != null) $this->palapa_client->where($where);
        if($limit != null && $offset != null) $this->palapa_client->limit($limit, $offset);
        return $this->palapa_client->get("pinjaman");
    }

    function get_all($limit, $uri) {
        $result = $this->palapa_client->get('pinjaman', $limit, $uri);
        if ($result->num_rows() > 0) {
            return $result->result_array();
        } else {
            return array();
        }
    }

    function get_one($id) {
        $this->palapa_client->select("anggota.*");
        $this->palapa_client->select("pinjaman.*");
        $this->palapa_client->select("jenis_kredit.*");

        $this->palapa_client->join("anggota", "anggota.ID_ANGGOTA = pinjaman.ID_ANGGOTA");
        $this->palapa_client->join("jenis_kredit", "jenis_kredit.ID_JENIS_KREDIT = pinjaman.ID_JENIS_KREDIT");

        $this->palapa_client->where('ID_PINJAMAN', $id);
        $result = $this->palapa_client->get('pinjaman');
        if ($result->num_rows() == 1) {
            return $result;
        } else {
            return array();
        }
    }

    function update_piutang($id_pinjaman_pusat, $piutang, $potongan, $margin)
    {
        $data = array(
                'PIUTANG' => $piutang,
                'POTONGAN' => $potongan,
                'MARGIN' => $margin,
        );

        $this->palapa_client = $this->load->database('palapa_client', TRUE);

        $this->palapa_client->where('ID_PINJAMAN_PUSAT', $id_pinjaman_pusat);
        return $this->palapa_client->update('pinjaman', $data);
    }

    function set_id_pinjaman_pusat($id, $new_id)
    {
        $this->palapa_client = $this->load->database('palapa_client', TRUE);
        $data = array(
                'ID_PINJAMAN_PUSAT' => $new_id,
        );
        $this->palapa_client->where('ID_PINJAMAN', $id);
        return $this->palapa_client->update('pinjaman', $data);
    }

    function approve($id) {
        $data = array(
                'STATUS' => 1,
                'ID_BUKTI_TRANSAKSI' => $this->input->post('BKK_BKB', TRUE)
        );

        $this->palapa_client->where('ID_PINJAMAN_PUSAT', $id);
        return $this->palapa_client->update('pinjaman', $data);
    }

    function reject($id) {
        $data = array(
                'STATUS' => -1,
                'ID_BUKTI_TRANSAKSI' => $this->input->post('BKK_BKB', TRUE)
        );

        $this->palapa_client->where('ID_PINJAMAN_PUSAT', $id);
        return $this->palapa_client->update('pinjaman', $data);
    }
}
?>